package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据商品分类查询规格组信息
     * @param cid 商品分类id
     * @return 规则组集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(
            @RequestParam("id") Long cid
            ){

        return ResponseEntity.ok(specGroupService.queryGroupByCategoryId(cid));

    }

    /**
     * 新增规格组
     * @param specGroupDTO 规格组信息
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO) {

        this.specGroupService.saveGroup(specGroupDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO){

        this.specGroupService.updateGroup(specGroupDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 删除规格项
     * @param id 规格id
     * @return
     */
    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable Long id) {

        this.specGroupService.deleteGroup(id);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 查询规格参数集合
     * @param cid 分类id
     * @param gid 规格组id
     * @param search 是否搜索
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(

            @RequestParam(value = "categoryId",required = false) Long cid,
            @RequestParam(value = "groupId",required = false) Long gid,
            @RequestParam(value = "searching",required = false) Boolean search

    ){

        return ResponseEntity.ok(specParamService.querySpecParams(cid,gid,search));

    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(

            @RequestBody SpecParamDTO specParamDTO

    ){

        this.specParamService.saveParam(specParamDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(

            @RequestBody SpecParamDTO specParamDTO

    ) {

        this.specParamService.updateParam(specParamDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 删除规格参数
     * @param id
     * @return
     */
    @DeleteMapping("/param")
    public ResponseEntity<Void> deleteParam(@RequestParam("id") Long id){

        this.specParamService.deleteParam(id);

        return ResponseEntity.status(HttpStatus.LOOP_DETECTED).build();

    }

    /**
     * 根据商品分类id查询规格组以及规格参数信息
     * @param cid 分类id
     * @return 返回结果
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(

            @RequestParam("id") Long cid

    ){

        return ResponseEntity.ok(this.specGroupService.querySpecList(cid));

    }

}
